package com.kaishun.study.controller;

import com.kaishun.study.Result.RoleMenuResult;
import com.kaishun.study.entity.TbRole;
import com.kaishun.study.entity.TbUser;
import com.kaishun.study.info.RoleMenuInfo;
import com.kaishun.study.service.RedisService;
import com.kaishun.study.service.TbRoleService;
import com.kaishun.study.service.TbUserService;
import com.kaishun.study.utils.PageRequest;
import com.kaishun.study.utils.PageResult;
import com.kaishun.study.utils.ResultVO;
import com.kaishun.study.utils.ResultVOUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * (TbRole)表控制层
 *
 * @author makejava
 * @since 2020-02-25 12:47:33
 */
@RestController
@RequestMapping("tbRole")
public class TbRoleController {
    /**
     * 服务对象
     */
    @Resource
    private TbRoleService tbRoleService;

    @Resource
    private TbUserService tbUserService;

    private Logger logger = LoggerFactory.getLogger(this.getClass());


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public TbRole selectOne(String id) {
        return this.tbRoleService.queryById(id);
    }

    /**************************begin**************************/

    @GetMapping("/getList")
    public ResultVO getUserList(Integer pageNum, Integer pageSize){
        logger.info("获取所有角色分页信息");
        ResultVO resultVO = new ResultVO();
        PageRequest pageRequest = new PageRequest();
        pageRequest.setPageNum(pageNum);
        pageRequest.setPageSize(pageSize);
        PageResult pageResult = tbRoleService.findPage(pageRequest);
        resultVO.setCode(0);
        resultVO.setMessage("获取角色渲染");
        resultVO.setCount((int)pageResult.getTotalSize());
        resultVO.setData(pageResult.getContent());
        return resultVO;
    }

    @DeleteMapping("/delete")
    public ResultVO deleteUserById(String id){
        logger.info("删除用户");
        tbRoleService.deleteById(id);
        return ResultVOUtil.success();
    }

    @PostMapping("/setRole")
    public ResultVO addUser(HttpServletRequest request, TbRole tbRole){
        logger.info("添加角色信息");
        tbRoleService.insert(tbRole,request);
        return ResultVOUtil.success();
    }

    @PutMapping("/setRole")
    public ResultVO updateUser(HttpServletRequest request ,TbRole tbRole){
        logger.info("更新角色信息");
        tbRoleService.update(tbRole,request);
        return ResultVOUtil.success();
    }








}